<style lang="less">
</style>
<template>
  <div class="search">
    <Row>
      <Card>
        <p slot="title">添加</p>
        <Form ref="form" :model="form" :label-width="90" :rules="formValidate">
          <FormItem label="新闻名称" prop="newsHeadlines">
            <Input v-model="form.newsHeadlines" style="width: 400px" />
          </FormItem>
          <Form-item label="新闻内容" prop="newsContent">
            <editor id="editor" v-model="form.newsContent" ref="editor"></editor>
          </Form-item>
          <Form-item>
            <Button
              @click="handleSubmit"
              :loading="submitLoading"
              type="primary"
              style="margin-right:5px"
            >提交并保存</Button>closeCurrentPage
            <Button @click="closeCurrentPage" type="primary" style="margin-right:5px">返回</Button>
          </Form-item>
        </Form>
      </Card>
    </Row>
  </div>
</template>

<script>
import editor from "@/views/my-components/xboot/editor";
export default {
  name: "add",
  components: {
    editor
  },
  data() {
    return {
      loading: true, // 表单加载状态
      submitLoading: false, // 表单提交状态
      form: {
        newsHeadlines: "",
        newsContent: ""
      },
      // 表单验证规则
      formValidate: {
        newsHeadlines: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        newsContent: [{ required: true, message: "不能为空", trigger: "blur" }]
      },
      backRoute: ""
    };
  },
  methods: {
    init() {
      this.backRoute = this.$route.query.backRoute;
    },
    handleReset() {
      this.$refs.form.resetFields();
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.postRequest("/journalism/add", this.form).then(res => {
            this.submitLoading = false;
            if (res.success) {
              this.$Message.success("添加成功");
              this.closeCurrentPage();
            }
          });
        }
      });
    },
    // 关闭当前页面
    closeCurrentPage() {
      this.$store.commit("removeTag", "add");
      localStorage.pageOpenedList = JSON.stringify(
        this.$store.state.app.pageOpenedList
      );
      this.$router.push({
        name: this.backRoute
      });
    }
  },
  watch: {
    $route(to, from) {
      if (to.name == "newsadd") {
        this.handleReset();
      }
    }
  },
  mounted() {
    this.init();
  }
};
</script>